package st.mark.quadtris.states;

import org.newdawn.slick.AppletGameContainer;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import st.mark.quadtris.BackgroundShapes;
import st.mark.quadtris.GameInfoData;
import st.mark.quadtris.MusicManager;
import st.mark.quadtris.Quadtris;

public class Menu extends BasicGameState {

	private int STATE_ID = -1;
	
	private String version = "v1.1 100909";
	
	// Menu Images
	private Image menu_bg = null;
	
	private Image play_quadtris = null;
	private Image play_normal = null;
	private Image highscores = null;
	private Image controls = null;
	private Image sound = null;
	private Image exit = null;
	private Image credits = null;
	private Image option_pointer = null;
	
	private Image controls_text = null;
	private Image credits_text = null;
	
	private Image sound_controls = null;
	private Image sound_pointer = null;
	
	private Image click_close = null;
	
	private Image loading_hiscores = null;
	
	private boolean show_controls = false;
	private boolean show_sound = false;
	private boolean show_credits = false;
	
	private float pointer_x = 0;
    private int pointer_x_max = 30;
    private int selected_option = 0;
    private boolean pointer_dir = false;
    
    private Sound click = null;
    private int last_selected = 0;
    
    private boolean isApplet = false;
    
    private BackgroundShapes bg;
    
	public Menu(int state){
		this.STATE_ID = state;
	}
	
	public int getID() {
		return STATE_ID;
	}

	public void enter(GameContainer gc, StateBasedGame sbg){
		GameInfoData.SHOW_SHAPES = true;
		if(!MusicManager.ready){
			try {
				MusicManager.init();
				MusicManager.ready = true;
			} catch (SlickException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		if(gc instanceof AppletGameContainer.Container)
			isApplet = true;
		
			
		// Load Images for the menu
		menu_bg = new Image("st/mark/resources/menu/menu_bg.png");
		
		play_quadtris = new Image("st/mark/resources/menu/play_quadtris.png");
		play_normal = new Image("st/mark/resources/menu/play_normal.png");
		highscores = new Image("st/mark/resources/menu/highscores.png");
		controls = new Image("st/mark/resources/menu/controls.png");
		sound = new Image("st/mark/resources/menu/sound.png");
		exit = new Image("st/mark/resources/menu/exit.png");
		credits = new Image("st/mark/resources/menu/credits.png");
		
		option_pointer = new Image("st/mark/resources/pausemenu/menu_pointer.png");
		
		controls_text = new Image("st/mark/resources/menu/controls-text.png");
		credits_text = new Image("st/mark/resources/menu/credits-text.png");
		
		sound_controls = new Image("st/mark/resources/pausemenu/sliders.png");
		sound_pointer = new Image("st/mark/resources/pausemenu/slider_bar.png");
		
		click_close = new Image("st/mark/resources/menu/click_to_close.png");
		
		loading_hiscores = new Image("st/mark/resources/highscores/loading.png");
		
		click = new Sound("st/mark/resources/audio/click.wav");
		
		bg = new BackgroundShapes();
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		// Bg Shapes
		if(GameInfoData.SHOW_SHAPES)
			bg.render();
		
		menu_bg.draw(0, 0);
		
		if(!show_controls && !show_sound && !show_credits){
			play_quadtris.draw(196, 115);
			play_normal.draw(196, 163);
			highscores.draw(196, 211);
			controls.draw(196, 259);
			sound.draw(196, 307);
			credits.draw(196, 355);
			if(!isApplet)
				exit.draw(196, 403);
			
			switch(selected_option){
				case 0: option_pointer.draw(125+pointer_x, 108); break;
				case 1: option_pointer.draw(125+pointer_x, 156); break;
				case 2: option_pointer.draw(125+pointer_x, 204); break;
				case 3: option_pointer.draw(125+pointer_x, 252); break;
				case 4: option_pointer.draw(125+pointer_x, 300); break;
				case 5: option_pointer.draw(125+pointer_x, 348); break;
				case 6: option_pointer.draw(125+pointer_x, 396); break;
			}
		}
		
		if(show_controls)
			controls_text.draw(91, 144);
		
		if(show_sound){
			sound_controls.draw(112, 200);
			
			sound_pointer.draw( (154 + (gc.getSoundVolume() * 100) * 1.55f) , 225);
			sound_pointer.draw( (479 + (gc.getMusicVolume() * 100) * 1.55f) , 225);
			
			click_close.draw(254, 400);
		}
		
		if(show_credits)
			credits_text.draw(91, 110);
		
		if(GameInfoData.SHOW_LOADING)
			loading_hiscores.draw(0, 0);
		
		g.setColor(Color.black);
		g.drawString(version, 10, 580);
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		MusicManager.update();
		Input input = gc.getInput();
		int mx = input.getMouseX(), my = input.getMouseY();
		
		// BG Shapes
		if(GameInfoData.SHOW_SHAPES)
			bg.update(delta);
		
		if(!show_controls && !show_sound && !show_sound){
			// Pointer X update
	    	if(pointer_dir){
	    		pointer_x += 0.05f * delta;
	    		
	    		if(pointer_x >= pointer_x_max){
	    			pointer_dir = !pointer_dir;
	    		}
	    	} else {
	    		pointer_x -= 0.05f * delta;
	    		
	    		if(pointer_x <= 0){
	    			pointer_dir = !pointer_dir;
	    		}
	    	}
	    	
	    	// Menu Mouse Options
	    	if(mx >= 196 && mx <= 196+play_quadtris.getWidth() && my >= 115 && my <= 115+play_quadtris.getHeight()){
	    		selected_option = 0;
	    		if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
	    			GameInfoData.MODE = GameInfoData.MODE_QUAD;
	    			sbg.enterState(Quadtris.GAMEPLAY_STATE);
	    		}
	    	}
	    	
	    	if(mx >= 196 && mx <= 196+play_normal.getWidth() && my >= 163 && my <= 163+play_normal.getHeight()){
	    		selected_option = 1;
	    		if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
	    			GameInfoData.MODE = GameInfoData.MODE_NORM;
	    			sbg.enterState(Quadtris.GAMEPLAY_STATE);
	    		}
	    	}
	    	
	    	if(mx >= 196 && mx <= 196+highscores.getWidth() && my >= 211 && my <= 211+highscores.getHeight()){
	    		selected_option = 2;
	    		if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
	    			GameInfoData.SHOW_LOADING = true;
	    			sbg.enterState(Quadtris.HIGHSCORES_STATE);
	    		}
	    	}
	    	
	    	if(mx >= 196 && mx <= 196+controls.getWidth() && my >= 259 && my <= 259+controls.getHeight()){
	    		selected_option = 3;
	    		if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON))
	    		show_controls = !show_controls;
	    	}
	    	
	    	if(mx >= 196 && mx <= 196+sound.getWidth() && my >= 307 && my <= 307+sound.getHeight()){
	    		selected_option = 4;
	    		if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON))
	    		show_sound = !show_sound;
	    	}
	    	
	    	if(mx >= 196 && mx <= 196+exit.getWidth() && my >= 355 && my <= 355+exit.getHeight()){
	    		selected_option = 5;
	    		if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON))
	    		show_credits = !show_credits;	
	    	}
	    	
	    	if(!isApplet && mx >= 196 && mx <= 196+credits.getWidth() && my >= 403 && my <= 403+credits.getHeight()){
	    		selected_option = 6;
	    		if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON))
	    			gc.exit();
	    	}
	    	
	    	// Menu Keyboard Options
	    	if(input.isKeyPressed(Input.KEY_UP)){
	    		selected_option--;
	    		
	    		if(selected_option < 0 && !isApplet)
	    			selected_option = 6;
	    		
	    		if(selected_option < 0 && isApplet)
	    			selected_option = 5;
	    	}
	    	
	    	if(input.isKeyPressed(Input.KEY_DOWN)){
	    		selected_option++;
	    		
	    		if(selected_option > 6 && !isApplet)
	    			selected_option = 0;
	    		
	    		if(selected_option > 5 && isApplet)
	    			selected_option = 0;
	    	}
	    	
	    	if(selected_option != last_selected){
	    		click.play();
	    		last_selected = selected_option;
	    	}
	    	
	    	// What to do with when enter is pressed
	    	if(input.isKeyPressed(Input.KEY_ENTER)){
		    	switch(selected_option){
					case 0: GameInfoData.MODE = GameInfoData.MODE_QUAD; sbg.enterState(Quadtris.GAMEPLAY_STATE); break;
					case 1: GameInfoData.MODE = GameInfoData.MODE_NORM; sbg.enterState(Quadtris.GAMEPLAY_STATE); break;
					case 2: GameInfoData.SHOW_LOADING = true; sbg.enterState(Quadtris.HIGHSCORES_STATE); break;
					case 3: show_controls = true; break;
					case 4: show_sound = true; break;
					case 5: show_credits = true; break;
					case 6: if(!isApplet) gc.exit(); break;
				}
	    	}
		}
		
		if((show_controls || show_credits) && (input.isMousePressed(Input.MOUSE_LEFT_BUTTON) || input.isKeyPressed(Input.KEY_ENTER))){
			show_controls = false;
			show_credits = false;
		}
		
		if(show_sound){
			if(mx >= 111 && mx <= 147 && my >= 243 && my <= 255 && input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)){
				gc.setSoundVolume(gc.getSoundVolume()-0.005f);
	    		
	    		if(gc.getSoundVolume() < 0.0f)
	    			gc.setSoundVolume(0.0f);
	    		
	    		if(!click.playing())
	    			click.play();
			}
			
			if(mx >= 328 && mx <= 358 && my >= 231 && my <= 267 && input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)){
				gc.setSoundVolume(gc.getSoundVolume()+0.005f);
	    		
	    		if(gc.getSoundVolume() > 1.0f)
	    			gc.setSoundVolume(1.0f);
	    		
	    		if(!click.playing())
	    			click.play();
			}
			
			if(mx >= 437 && mx <= 473 && my >= 243 && my <= 255 && input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)){
				gc.setMusicVolume(gc.getMusicVolume()-0.005f);
	    		
	    		if(gc.getMusicVolume() < 0.0f)
	    			gc.setMusicVolume(0.0f);
			}
			
			if(mx >= 653 && mx <= 689 && my >= 231 && my <= 267 && input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)){
				gc.setMusicVolume(gc.getMusicVolume()+0.005f);
	    		
	    		if(gc.getMusicVolume() > 1.0f)
	    			gc.setMusicVolume(1.0f);
			}
			
			//click_close.draw(254, 400);
			if(mx >= 254 && mx <= 254+click_close.getWidth() && my >= 400 && my <= 400+click_close.getHeight() && input.isMousePressed(Input.MOUSE_LEFT_BUTTON))
				show_sound = false;
		}
	}
}
